package com.manage.service;

import com.manage.ManageConstants;
import com.manage.mapper.UserBaseMapper;
import com.manage.mapper.UserDetailsMapper;
import com.manage.mapper.UserExtensionsMapper;
import com.manage.pojo.UserBase;
import com.manage.pojo.UserDetails;
import com.manage.pojo.UserExtensions;
import com.manage.pojo.UserSms;
import com.manage.result.Result;
import com.manage.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.persistence.Transient;
import java.util.List;

import static com.manage.result.CodeMsg.*;

@Service
public class UserService {
    @Autowired
    UserDetailsMapper userDetailsMapper;
    @Autowired
    UserBaseMapper userBaseMapper;
    @Autowired
    UserExtensionsMapper userExtensionsMapper;
    @Autowired
    UserSmsService mUserSmsService;

    @Transient
    public void saveUser(UserBase userBase, UserDetails userDetails, UserExtensions userExtensions) {

        userBaseMapper.insert(userBase);

        userDetailsMapper.insert(userDetails);

        userExtensionsMapper.insert(userExtensions);

        userBaseMapper.insertUserBaseAndUserDetails(userBase.getJobNumber(), userDetails.getId());

        userBaseMapper.insertUserBaseAndUserExtensions(userBase.getJobNumber(), userExtensions.getId());

    }

    public void updateUser(UserBase userBase, UserDetails userDetails, UserExtensions userExtensions) {

        Example userBaseExample = new Example(userBase.getClass());
        //sql语句后面拼接jobnumber==？？？
        userBaseExample.createCriteria().andEqualTo("jobNumber", userBase.getJobNumber());

        Example userDetailsExample = new Example(userDetails.getClass());

        userDetailsExample.createCriteria().andEqualTo("id", userDetails.getId());

        Example userExtensionsExample = new Example(userExtensions.getClass());

        userExtensionsExample.createCriteria().andEqualTo("id", userExtensions.getId());

        userBaseMapper.updateByExample(userBase, userBaseExample);
        userDetailsMapper.updateByExample(userDetails, userDetailsExample);
        userExtensionsMapper.updateByExample(userExtensions, userExtensionsExample);

    }

    //通过工号查询员工信息
    public UserVo selectUser(Long jobNumber) {

        UserBase userBase = userBaseMapper.selectByPrimaryKey(jobNumber);
        if (userBase == null) {
            //todo:数据异常处理
        }
        UserDetails userDetail = userDetailsMapper.queryByJobNumber(jobNumber);
        if (userDetail == null) {
            //todo:数据异常处理
        }
        UserExtensions userExtensions = userExtensionsMapper.queryByJobNumber(jobNumber);
        if (userExtensions == null) {
            //todo:数据异常处理
        }
        UserVo userVo = new UserVo();
        userVo.setUserBase(userBase);
        userVo.setUserDetails(userDetail);
        userVo.setUserExtensions(userExtensions);
        return userVo;
    }

    //通过手机号查询员工信息
    public Result<UserVo> queryUserByMobilePhone(String mobilePhone) {
        Example userDetailExample = new Example(UserDetails.class);
        Example.Criteria userDetailExampleCriteria = userDetailExample.createCriteria();
        userDetailExampleCriteria.andEqualTo("mobilePhone", mobilePhone);
        List<UserDetails> userDetailsList = null;
        try {
            userDetailsList = userDetailsMapper.selectByExample(userDetailExample);
        } catch (Exception e) {
            return Result.error(DB_SERVER_ERROR);
        }
        if (userDetailsList == null || userDetailsList.isEmpty()) {
            return Result.error(PHONE_HAS_NOT_BEEN_REGISTERED);
        }
        if (userDetailsList.size() > 1) {
            return Result.error(PHONE_HAS_BEEN_MANY);
        }
        return Result.success(selectUser(userDetailsList.get(0).getId()));
    }

    //todo:后面查询需要优化，封装方法优化，例如封装根据id查询或者根据mobilePhone查询

    public Result<UserVo> passwordLogin(String mobilePhone, String password) {
        Example userDetailExample = new Example(UserDetails.class);
        Example.Criteria userDetailExampleCriteria = userDetailExample.createCriteria();
        userDetailExampleCriteria.andEqualTo("mobilePhone", mobilePhone);
        List<UserDetails> userDetailsList = null;
        try {
            userDetailsList = userDetailsMapper.selectByExample(userDetailExample);
        } catch (Exception e) {
            return Result.error(DB_SERVER_ERROR);
        }
        if (userDetailsList == null || userDetailsList.isEmpty()) {
            return Result.error(PHONE_HAS_NOT_BEEN_REGISTERED);
        }
        if (userDetailsList.size() > 1) {
            return Result.error(PHONE_HAS_BEEN_MANY);
        }

        Example userExtensionsExample = new Example(UserExtensions.class);
        Example.Criteria userExtensionsExampleCriteria = userExtensionsExample.createCriteria();
        userExtensionsExampleCriteria.andEqualTo("id", userDetailsList.get(0).getId());
        List<UserExtensions> userExtensionsList = null;
        try {
            userExtensionsList = userExtensionsMapper.selectByExample(userExtensionsExample);
        } catch (Exception e) {
            return Result.error(DB_SERVER_ERROR);
        }
        if (userExtensionsList == null || userExtensionsList.isEmpty()) {
            //todo:空值处理
        }
        if (userExtensionsList.size() > 1) {
            //todo:数据异常处理
        }
        if (password.equals(userExtensionsList.get(0).getPassword())) {
            UserVo userVo = selectUser(userDetailsList.get(0).getId());
            userVo.setUserDetails(userDetailsList.get(0));
            userVo.setUserExtensions(userExtensionsList.get(0));
            return Result.success(userVo);
        } else {
            return Result.error(PASSWORD_ERROR);
        }

    }

    public Result deleteUser(Long jobNumber) {
        //不想写sql语句，我先查询在删除
        try {
            UserDetails userDetails = userDetailsMapper.queryByJobNumber(jobNumber);
            UserExtensions userExtensions = userExtensionsMapper.queryByJobNumber(jobNumber);
            //删除
            userBaseMapper.deleteByPrimaryKey(jobNumber);
            userBaseMapper.deleteUserBaseAndDetails(jobNumber);
            userBaseMapper.deleteUserBaseAndExtension(jobNumber);
            userDetailsMapper.deleteByPrimaryKey(userDetails.getId());
            userExtensionsMapper.deleteByPrimaryKey(userExtensions.getId());
        } catch (Exception e) {
            return Result.error(DB_SERVER_ERROR);
        }

        return Result.success(null);
    }

    public Result<UserVo> fastLogin(String mobilePhone, long smsType, String smsCode) {
        List<UserSms> userSmsList = mUserSmsService.querySmsByMobilePhone(mobilePhone);
        if (userSmsList == null || userSmsList.isEmpty()) {
            return Result.error(SMS_IS_ERROR);
        }
        for (UserSms item : userSmsList) {
            //todo:需要判断时间是否在当前五分钟之前
            if (ManageConstants.OPERATE_SUCCESSFULLY != item.getUsedState()) {
                continue;
            }

            if (!item.getSmsType().equals(smsType)) {
                continue;
            }

            if (item.getSmsCode().equals(smsCode)) {
                //快速登录成功
                item.setUsedState(1L);
                if (ManageConstants.OPERATE_SUCCESSFULLY == mUserSmsService.updateSms(item)) {
                    return queryUserByMobilePhone(mobilePhone);
                } else {
                    return Result.error(Send_SMS_DEL_ERROR);
                }
            }
        }

        return Result.error(SMS_IS_ERROR);
    }
}